<?php

namespace app\controller\admin\module\banip;

use app\controller\admin\module\AdminBaseController;
use think\exception\ValidateException;
use think\response\Json;
use think\response\View;

/**
 * @auth-name 封禁IP
 * @auth-alias banip
 */
class Index extends AdminBaseController
{
    protected string $modelName = 'accessIp';

    /**
     * @auth-name 主页
     * @auth-alias index
     * @auth-request-method post
     * @return View
     */
    public function index(): View
    {
        return fetch('/admin/ban_ip/index');
    }


    /**
     * @auth-name 列表
     * @auth-alias list
     * @return Json
     */
    public function list(): Json
    {
        $limit = values('limit', 10);
        $this->model->customLeft = [
            'table' => 'admin',
            'on' => 'admin.id = '.$this->modelName.'.aid'
        ];
        $condition = [];
        $aid = values('aid', 0);
        $ip = values('ip');
        !empty($aid) and $condition['aid'] = $aid;
        !empty($ip) and $condition['ip'] = $ip;
        return json($this->model->getPageList($condition, $this->modelName.'.id, ip, '.$this->modelName.'.addtime, aid, name', $this->modelName.'.addtime desc', ['start' => ((values('page', 10) - 1) * $limit), 'limit' => $limit]));
    }

    /**
     * @auth-name 添加
     * @auth-alias add
     * @return View
     */
    public function add(): View
    {
        return fetch('/admin/ban_ip/add');
    }


    /**
     * @auth-name 添加提交
     * @auth-alias add
     * @auth-request-method post
     * @return Json
     */
    public function addPost(): Json
    {
        $ip = values('post.ip');   //IP地址
        try {
            $this->validate([
                'ip' => $ip,
            ],[
                'ip|IP地址' => ['require', 'ip'],
            ]);
            $repeatID = $this->model->getInfo(['ip' => $ip], 'id');
            if(!empty($repeatID)){
                return responseMsg('IP地址已经存在');
            }
            $lastId = $this->model->add([
                'ip' => $ip,
                'aid' => $this->admin['id'],
                'addtime' => time(),
            ], true);
            if($lastId > 0){
                putAdminLog("新增封禁IP地址[$lastId]");
                setCaches('BanIP' , $this->model->getList([], 'ip'));
                return responseMsg('新增成功', 200);
            }
            return responseMsg('新增失败');
        }catch (ValidateException $e){
            return responseMsg($e->getError());
        }
    }


    /**
     * @auth-name 编辑页面
     * @auth-alias edit
     * @return View
     */
    public function edit(): View
    {
        $id = values('get.id', 0);
        empty($id) and missPage();
        $info = $this->model->getInfo(['id' => $id], 'id, ip');
        empty($info) and errorData();
        return fetch('/admin/ban_ip/edit', [
            'info' => $info,
        ]);
    }

    /**
     * @auth-name 编辑提交
     * @auth-alias edit
     * @auth-request-method post
     * @return Json
     */
    public function editPost(){
        $id = values('post.id', 0);
        if(empty($id)){
            return responseMsg('缺少必要参数，请重新进入编辑页面');
        }
        $infoID = $this->model->getInfo(['id' => $id], 'id');
        if(empty($infoID)){
            return responseMsg('未找到相关信息');
        }
        $ip = values('post.ip');   //IP地址
        try {
            $this->validate([
                'ip' => $ip,
            ],[
                'ip|IP地址' => ['require', 'ip'],
            ]);
            $repeatID = $this->model->getInfo(['ip' => $ip], 'id');
            if(!empty($repeatID) and $repeatID != $infoID){
                return responseMsg('IP地址已经存在');
            }
            if($this->model->edit(['id' => $id], [
                'ip' => $ip,
                'aid' => $this->admin['id'],
            ])){
                putAdminLog("编辑封禁IP地址[$id]");
                setCaches('BanIP' , $this->model->getList([], 'ip'));
                return responseMsg('编辑成功', 200);
            }
            return responseMsg('编辑失败');
        }catch (ValidateException $e){
            return responseMsg($e->getError());
        }
    }

    /**
     * @auth-name 删除
     * @auth-alias del
     * @auth-request-method delete
     * @return Json
     */
    public function del(){
        $idx = values('idx', []);
        if(!empty($idx)){
            $list = $this->model->getList(['id' => $idx]);
            if(empty($list)){
                return responseMsg('未找到IP地址相关信息');
            }
            if($this->model->del(['id' => $idx])){
                putAdminLog("删除IP地址[" . json_encode($list) . "]");
                setCaches('BanIP' , $this->model->getList([], 'ip'));
                return responseMsg('操作成功', 200);
            }
        }
        return responseMsg('操作失败，请稍后重试');
    }
}